﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ExchangeHog.EHCL
{
    class EHCL
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Console.WriteLine(
@"Usage: EHCL [<arguments>]
    where arguments is one of
        -add_source <POP3|IMAP> <host> <port> <userid>
            Adds source 
        -add_target <host> <port> <userid>
            Adds IMAP target
        -run (default)
            Checks the messages");
            }
            else
            {
    			String appData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
		    	String fnConfig = Path.Combine(appData, "EHCore.cfg");
                var ehcore = new EHCore.EHCore();
                ehcore.loadConfiguration(fnConfig);
                switch (args[0].ToLower())
                {
                    case "-add_source":
                    case "-add_target":
                        if (args.Length < 2)
                            Console.WriteLine("Invalid number of arguments");
                        else
                        {
                            EHCore.ConnectInfo ci = null;
                            try
                            {
                                ci = new EHCore.ConnectInfo(new ArraySegment<String>(args, 1, args.Length - 1).ToArray<String>());
                            }
                            catch (ArgumentException ae)
                            {
                                Console.WriteLine("Invalid arguments: " + ae);
                            }
                            if (ci != null)
                            {
                                if (args[0].ToLower().Equals("-add_source"))
                                    ehcore.config.sources.Add(ci);
                                else
                                    ehcore.config.target = ci;
                                ehcore.saveConfiguration(fnConfig);
                            }
                        }
                        break;
                    case "-run":
                    default:
                        ehcore.check();
                        break;
                }
            }
        }
    }
}
